<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class tool extends CI_Controller {
var $cookieFileName;
	function __construct() {
		
        parent::__construct();
		//$this->load->model("getsite/tvzing");
		$this->load->library('get_data_html');
		$this->cookieFileName = dirname (realpath(__FILE__)).'/'.$this->cookieFileName; 
    }
    public function field($table_name = '') {
        $fields = $this->db->field_data($table_name);
        echo '$fieldData = array();';
        echo "<br>";
        foreach ($fields as $field) {
            if ($field->default == '')
                $data = $this->fieldDefault($field->name, $field->type);
            else
                $data = $field->default;
            echo '$fieldData[\'' . $field->name . '\'] = ' . $data . ';';
            echo "<br>";
        }
        echo 'return $fieldData;';
    }

    function fieldDefault($name, $type = '') {
        $default = "";

        //time
        if (strpos($name, "TIME_"))
            $default = 'time()';
        else if (strpos($name, "_STATUS"))
            $default = '1';
        else if ($type == 'bigint' || $type == 'int')
            $default = '0';
        else
            $default = "''";
        return $default;
    }

    function addProjectPermisionDefault() {
        $arr_code = array('PROJECT', 'TASK', 'TODO');
    }

    function addUserPermisionDefault($userID) {
        $arr_code = array('PROJECT', 'TASK', 'TODO');
        foreach ($arr_code as $code) {
            $array_insert = array();
            $array_insert['RES_CODE'] = $code;
            $array_insert['RES_NAME'] = $code;

            $array_insert['USE_ID'] = $userID;
        }
    }
	
	function check_ref(){
		$this->load->library('user_agent');
		echo '<pre>';
				var_dump($_SERVER);
				echo '</pre>';
	}
	
	function countsoft(){
		$url = 'http://www.download.com.vn/windows';
		$html_content = $this->get_data_html->get_content($url);
		$array = array('type' => '1',
            'estart' => '<ul id="leftnavigation" class="leftnavigation">',
            'eend' => '<\/div>',
        );
		$cates = $this->get_data_html->load($html_content, $array);
		$cates = $this->get_data_html->removetag('<ul class=\'clearfix\'>', '<\/ul>',$cates[0]);
		//$cates = $this->get_data_html->removetag('<ul>', '<\/ul>',$cates);
		$cates = strip_tags($cates,'<a>');
		$array = array('type' => '1',
            'estart' => 'href="',
            'eend' => '"',
        );
		$cates = $this->get_data_html->load($cates, $array);
		foreach($cates as $cate){
			$url = 'http://www.download.com.vn'.$cate;
			$html_content = $this->get_data_html->get_content($url);
			$tcount = $this->get_data_html->GetBetween($html_content, '<div class="summary-info">', '</div>');
			if($tcount =='')
				$tcount = 10;
			$tcount = str_replace(array('Có tất cả','phần mềm','.'),"",$tcount);
			$tcount = trim($tcount);
			echo str_replace(array('/','index.aspx'),"",$cate).": ".$tcount."<br>";
			
		}
		
		//echo $html_content;
	}
	
	function updatesoftcache(){
	set_time_limit(9999999999);
	$this->load->model("msoft");
	$this->load->model("mcategory");
	$cates = $this->mcategory->gets();
	foreach ($cates as $cate){
		$this->msoft->update_cache_cate($cate->CAT_ID);
		}
	}
	function fixcatcode3(){
	
		$this->load->model("mcategory");
		$this->db->order_by("CAT_PARENT asc");
		$cates = $this->mcategory->gets(array('CAT_PARENT >'=>'0'));
		foreach ($cates as $cate){
			$is_catlv3 = $this->mcategory->get(array('CAT_ID'=> $cate->CAT_PARENT,'CAT_PARENT >'=>'0' ));
			if($is_catlv3) {
				$arr_update = array();
				$arr_update['CAT_CODE'] = $is_catlv3->CAT_CODE.'-'.$this->seo->seolink($cate->CAT_NAME);
				$arr_update['CAT_CODE'] = strtolower($arr_update['CAT_CODE']);
				echo $arr_update['CAT_CODE'].' - '.$cate->CAT_ID."<br>";
				$this->mcategory->update($cate->CAT_ID,'CAT_ID',$arr_update);
			}
		}
	}
	function fixcatcode2(){
		$this->load->model("mcategory");
		$this->db->order_by("CAT_PARENT asc");
		$cates = $this->mcategory->gets(array('CAT_PARENT >'=>'0'));
		foreach ($cates as $cate){
			$is_catlv2 = $this->mcategory->get(array('CAT_ID'=> $cate->CAT_PARENT,'CAT_PARENT'=>'0' ));
			if($is_catlv2) {
				$arr_update = array();
				$arr_update['CAT_CODE'] = $is_catlv2->CAT_CODE.'-'.$this->seo->seolink($cate->CAT_NAME);
				$arr_update['CAT_CODE'] = strtolower($arr_update['CAT_CODE']);
				echo $arr_update['CAT_CODE']."<br>";
				$this->mcategory->update($cate->CAT_ID,'CAT_ID',$arr_update);
			}
		}
	
	}
	function fixcode(){
		$this->load->model("mcategory");
		$this->db->order_by("CAT_PARENT asc");
		$cates = $this->mcategory->gets();
		foreach ($cates as $cate){
			$arr_update = array();
			$arr_update['CAT_NAME'] = $this->seo->DecimalToUni($cate->CAT_NAME);

			$arr_update['CAT_CODE'] = $this->seo->seolink($arr_update['CAT_NAME']);
			if($cate->CAT_PARENT >0){
			$catcode = $this->getRootParent($cate->CAT_PARENT);
				$arr_update['CAT_CODE'] = $catcode."-".$arr_update['CAT_CODE'];
			}
			$arr_update['CAT_CODE'] = strtolower($arr_update['CAT_CODE']);
			$this->mcategory->update($cate->CAT_ID,'CAT_ID',$arr_update);
			
		}
	}
	
	function getRootParent($parentID, $catcode = ''){
		$tcate = $this->mcategory->get($parentID,'CAT_ID');
		if($tcate && $tcate->CAT_PARENT ==0){
			return $tcate->CAT_CODE;
		}
		 return $this->getRootParent($tcate->CAT_PARENT);
	}
	
	function xxxx(){
	
	echo $this->seo->DecimalToUni("Đồ hoạ m&#225;y t&#237;nh");
	}
	
	function parse_images_step1_cate(){
	
	//category
	$this->db->where("CAT_THUMB !=''");
	$result = $this->db->get("category")->result_array();
	
	$arr_insert = array();
	foreach($result as $row){
		$arr_insert['GETI_CODE'] = 'category.CAT_THUMB.'.$row['CAT_ID'];
		$arr_insert['GETI_LINK_OLD'] = $row['CAT_THUMB'];
		$arr_insert['GETI_NAME_NEW'] = $row['CAT_CODE'];
		$this->db->insert("getimages",$arr_insert);
	}
	
	//manufacturer
	$this->db->where("MAN_THUMB !=''");
	$result = $this->db->get("manufacturer")->result_array();
	//$result = mysql_query("select * from manufacturer where MAN_THUMB !=''");
	$arr_insert = array();
	foreach($result as $row){
		$arr_insert['GETI_CODE'] = 'manufacturer.MAN_THUMB.'.$row['MAN_ID'];
		$arr_insert['GETI_LINK_OLD'] = $row['MAN_THUMB'];
		$arr_insert['GETI_NAME_NEW'] = $row['MAN_CODE'];
		$this->db->insert("getimages",$arr_insert);
	}
	
	
	
	}
	
	function parse_images_step1_soft($page = 0){
		$num = 20;
		if($page <=0)
			$page = 0;
		$from = ($page)*$num;
		$this->db->limit($num,$from);
		$result = $this->db->get("soft")->result_array();
	
		$arr_insert = array();
		if(!$result)
			die("X");
		
		foreach($result as $row){
			echo $row['SOFT_ID']." - ".$row['SOFT_NAME']."<br>";
			$arr_insert['GETI_CODE'] = 'soft.SOFT_THUMB.'.$row['SOFT_ID'];
			$arr_insert['GETI_LINK_OLD'] = $row['SOFT_THUMB'];
			$arr_insert['GETI_NAME_NEW'] = $row['SOFT_CODE'];
			$check = $this->db->from("getimages")->where("GETI_CODE",$arr_insert['GETI_CODE'])->get()->result();
			if(!$check)
				$this->db->insert("getimages",$arr_insert);

			
			$arr_insert['GETI_CODE'] = 'soft.SOFT_ICON.'.$row['SOFT_ID'];
			$arr_insert['GETI_LINK_OLD'] = $row['SOFT_ICON'];
			$arr_insert['GETI_NAME_NEW'] = $row['SOFT_CODE'];
			$check = $this->db->from("getimages")->where("GETI_CODE",$arr_insert['GETI_CODE'])->get()->result();
			if(!$check)
				$this->db->insert("getimages",$arr_insert);
			
			
			//get SOFT_CONTENT_SHORT
			$content =  $this->seo->decode_tag_html($row['SOFT_CONTENT_SHORT']);
			$content = trim(strip_tags($content,'<img>'));
			$array = array('type' => '1',
						'estart' => 'src="',
						'eend' => '"',
					);
			$images = $this->get_data_html->load($content, $array);

			$array = array('type' => '1',
						'estart' => "src='",
						'eend' => "'",
					);
			$images2 = $this->get_data_html->load($content, $array);
			foreach($images2 as $r){
				$images[] = $r;
			}
			if(count($images)>0)
			foreach($images as $image){
			if($image == '')
				continue;
			$arr_insert['GETI_CODE'] = 'soft.SOFT_CONTENT_SHORT.'.$row['SOFT_ID'];
				$arr_insert['GETI_LINK_OLD'] = $image;
				$arr_insert['GETI_NAME_NEW'] = $row['SOFT_CODE'];
				
				$check = $this->db->from("getimages")->where("GETI_CODE",$arr_insert['GETI_CODE'])->where("GETI_LINK_OLD",$image)->get()->result();
				if(!$check)
					$this->db->insert("getimages",$arr_insert);
			}

		
		
		//get SOFT_CONTENT
		$content =  $this->seo->decode_tag_html($row['SOFT_CONTENT']);
		$content = trim(strip_tags($content,'<img>'));
		$array = array('type' => '1',
					'estart' => 'src="',
					'eend' => '"',
				);
		$images = $this->get_data_html->load($content, $array);

		$array = array('type' => '1',
					'estart' => "src='",
					'eend' => "'",
				);
		$images2 = $this->get_data_html->load($content, $array);
		foreach($images2 as $r){
			$images[] = $r;
		}
		if(count($images)>0)
		foreach($images as $image){
		if($image == '')
			continue;
		$arr_insert['GETI_CODE'] = 'soft.SOFT_CONTENT.'.$row['SOFT_ID'];
			$arr_insert['GETI_LINK_OLD'] = $image;
			$arr_insert['GETI_NAME_NEW'] = $row['SOFT_CODE'];
			
			$check = $this->db->from("getimages")->where("GETI_CODE",$arr_insert['GETI_CODE'])->where("GETI_LINK_OLD",$image)->get()->result();
				if(!$check)
			$this->db->insert("getimages",$arr_insert);
		}

			
		}
	$newpage = $page +1;
		echo '<meta http-equiv="refresh" content="1;url='.base_url().'tool/parse_images_step1_soft/'.$newpage.'">';
	}
	
	/*
	function count_game24h_vn(){

		$url = 'http://www.game24h.vn';
		$html_content = $this->get_data_html->googlebot_get($url);
		$cates = $this->get_data_html->GetBetween($html_content, '<font color="#ff0000">','<div class="leftadv">');
		
		$array = array('type' => '1',
            'estart' => '<div class="dinline cat-item">',
            'eend' => '<\/div>',
        );
		$cates = $this->get_data_html->load($cates, $array);
		
		$arr_info = array();
		for($i=0; $i<count($cates); $i++){
			$cate = $cates[$i];
			$cate = trim(strip_tags($cate,'<a>'));
			$link = $this->get_data_html->GetBetween($cate, 'href="','"');
			$link = 'http://www.game24h.vn'.trim($link);
			
			$title =  trim(strip_tags($cate));
			
			$count = $this->count_game24h_vn_cate($link);
			if($count =='')
				$count = 1;
			$countGameEndPage = $this->count_page_end($link.'/'.$count);
			$count = $count*20*4 - 80 +$countGameEndPage;
			$arr_info[$i] = array();
			$arr_info[$i]['link'] = $link;
			$arr_info[$i]['title'] = $title;
			$arr_info[$i]['total'] = $count;
		}
		$total = 0;
		for($i=0; $i<count($arr_info); $i++){
			echo '<a href="'.$arr_info[$i]['link'].'" target="_blank">'.$arr_info[$i]['title'].'</a>: '.$arr_info[$i]['total']."<br>";
			$total+= $arr_info[$i]['total'];
		}
		echo "Total: ".$total;
	}
	
	function count_game24h_vn_cate($url){
		$html_content = $this->get_data_html->googlebot_get($url);
		$cates = $this->get_data_html->GetBetween($html_content, '<div align="center" id="pagination">','<div class="bottom_border">');
		$array = array('type' => '1',
            'estart' => '<a href="',
            'eend' => '"',
        );
		$cates = $this->get_data_html->load($cates, $array);
		$cate = end($cates);
		$catex = explode("/",$cate);
		$catek = end($catex);
		return $catek;
		
	}
	
	function count_page_end($url){
	$html_content = $this->get_data_html->googlebot_get($url);
	$array = array('type' => '1',
            'estart' => '<div class="games-item">',
            'eend' => '<\/div>',
        );
		$games = $this->get_data_html->load($html_content, $array);
		return count($games);
	}
	
	*/
	function count_game24h_vn(){
	
		$url = 'http://game.24h.com.vn/';
		$html_content = $this->get_data_html->googlebot_get($url);
		$cates = $this->get_data_html->GetBetween($html_content, '<div class="menu-game">','<div class="menu-top">');
		
		$array = array('type' => '1',
            'estart' => '<li',
            'eend' => '<\/li>',
        );
		$cates = $this->get_data_html->load($cates, $array);
		unset($cates[9]);
		
		$arr_info = array();
		$i = 0;
		foreach($cates as $cate){
		//for($i=0; $i<count($cates); $i++){
			//$cate = $cates[$i];
			$cate = trim(strip_tags($cate,'<a>'));
			$link = $this->get_data_html->GetBetween($cate, 'href="','"');
			$link = 'http://game.24h.com.vn'.trim($link);
			
			$title =  trim(strip_tags($cate));
			$title = str_replace(array(">",'style="text-align:left"'),"",$title);
			$title = trim($title);
			
			$ids = explode("-",$link);
			$id = end($ids);
			$id = str_replace(array('.html','c'),"",$id);
			$url = 'http://game.24h.com.vn/ajax/game_hay_level1.php?type=hay_nhat&menu_id='.$id.'&page=1';
			
			
			$count = $this->count_game24h_vn_cate($url);
			if($count =='')
				$count = 1;
			
			$url = 'http://game.24h.com.vn/ajax/game_hay_level1.php?type=hay_nhat&menu_id='.$id.'&page='.$count;
			$countGameEndPage = $this->count_page_end($url);
			$count = $count*5*2 - 10 +$countGameEndPage;
			$arr_info[$i] = array();
			$arr_info[$i]['link'] = $link;
			$arr_info[$i]['title'] = $title;
			$arr_info[$i]['total'] = $count;
			$i++;
		}
		$total = 0;
		for($i=0; $i<count($arr_info); $i++){
			echo '<a href="'.$arr_info[$i]['link'].'" target="_blank">'.$arr_info[$i]['title'].'</a>: '.$arr_info[$i]['total']."<br>";
			$total+= $arr_info[$i]['total'];
		}
		echo "Total: ".$total;
	}
	
	function count_game24h_vn_cate($url){
		$html_content = $this->get_data_html->googlebot_get($url);
		$cates = $this->get_data_html->GetBetween($html_content, '<div class="pageList">','</div>');
		$array = array('type' => '1',
            'estart' => '<a href="',
            'eend' => '"',
        );
		$cates = $this->get_data_html->load($cates, $array);
		
		
		$cate = end($cates);
		$catex = explode("=",$cate);
		
		$catek = end($catex);
		$catek = str_replace("');","",$catek);
		return $catek;
		
	}
	
	function count_page_end($url){
	$html_content = $this->get_data_html->googlebot_get($url);
	$array = array('type' => '1',
            'estart' => '<div class="gameItem"',
            'eend' => '<\/div>',
        );
		$games = $this->get_data_html->load($html_content, $array);
		return count($games);
	}
	
	function php(){
	echo phpinfo();
	}
}
